﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace Excellent
{
    class DBHelper
    {
        public static string conString = "Data Source=.;Initial Catalog=excellence;Integrated Security=True";
        public static SqlConnection con = null;
        public static void InitConnection()
        {
            //如果没有连接，创建连接
            if (con == null)
            {
                con = new SqlConnection(conString);
            }
            //如果连接中断，重启连接
            if (con.State == ConnectionState.Broken)
            {
                con.Close();
                con.Open();
            }
            //如果连接关闭，打开连接
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
        }
        //查询
        public static DataSet GetDataSet(string sql)
        {
            InitConnection();
            //创建数据集
            DataSet ds = new DataSet();
            SqlDataAdapter ad = new SqlDataAdapter(sql, con);
            ad.Fill(ds);
            return ds;
        }
        //增删改
        public static bool ExecuteNonQuery(string sql)
        {
            InitConnection();
            SqlCommand com = new SqlCommand(sql, con);
            int row = com.ExecuteNonQuery();
            return row>0;
        }
    }
}
